package manager_web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import manager_service.service.BackCourseTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import test_common.constant.Constant;
import test_common.entity.CourseType;
import test_common.utils.ContentIsNull;
import test_common.utils.ResponseResult;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhangzheng
 * @create 2021/2/1
 */
@Controller
@RequestMapping("/courseType")
public class CourseTypeController {

    @Reference
    private BackCourseTypeService backCourseTypeService;

    @RequestMapping("/addCourseType")
    public ResponseResult addCourseType(HttpServletRequest request){
        ResponseResult result=new ResponseResult();
        String courseTypeName=request.getParameter("courseTypeName");
        String parentId=request.getParameter("parentId");
        CourseType courseType1=new CourseType();
        if (parentId.isEmpty()){
            courseType1.setParentId(Constant.PARENTID_NULL);
            courseType1.setStatus(Constant.COURSETYPE_ENABLE);
            courseType1.setTypeName(courseTypeName);
        }
        else{
            courseType1.setParentId(Integer.parseInt(parentId));
            courseType1.setStatus(1);
            courseType1.setTypeName(courseTypeName);
        }
        backCourseTypeService.addCourseType(courseType1);
        result.setStatus(Constant.RESPONSE_SUCCESS);
        result.setMessage("添加成功");
        return  result;
    }

    @RequestMapping("/showCourseTypes")
    public ModelAndView showCourseTypes(HttpServletRequest request){
        ModelAndView mav=new ModelAndView();
        String pageNo=request.getParameter("pageNo");
        String pageSize=request.getParameter("pageSize");
        String parentId=request.getParameter("parentId");

        //如果前端没有传递，使用默认值
        if (ContentIsNull.isNull(pageSize)) {
            pageSize= Constant.Course_DEFAULT_PAGE_SIZE;
        }
        if (ContentIsNull.isNull(pageNo)){
            pageNo= Constant.Course_DEFAULT_PAGE_NO;
        }
        int pageNoint=Integer.parseInt(pageNo);
        int pageSizeint=Integer.parseInt(pageSize);
        Integer parentIdInt=null;
        PageInfo<CourseType> typePageInfo =null;
        if (parentId.isEmpty()){
            parentIdInt=0;
           typePageInfo=backCourseTypeService.showCourses(pageNoint,pageSizeint,parentIdInt);
        }
        else {
            typePageInfo=backCourseTypeService.showCourses(pageNoint,pageSizeint,Integer.parseInt(parentId));
        }

        mav.addObject("typePageInfo",typePageInfo);
        mav.addObject("pageNo",pageNoint);
        mav.setViewName("/back_courseTypeSet");
        return  mav;
    }

    @RequestMapping("/modifyStatus")
    public ResponseResult modifyStatus(HttpServletRequest request){
        ResponseResult result=new ResponseResult();
        String courseTypeId=request.getParameter("courseTypeId");
        String pageNo=request.getParameter("pageNo");
        backCourseTypeService.modifyStatus(courseTypeId);
        result.setStatus(Constant.RESPONSE_SUCCESS);
        return result;

    }
}
